package cn.gson.springboot.controllers;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.springboot.model.mapper.DrugRoomMappers;
import cn.gson.springboot.model.pojos.ClinicCard;
import cn.gson.springboot.model.service.DrugRoomService;
import cn.gson.springboot.model.vo.AdviceVo;
import cn.gson.springboot.model.vo.DestructionVo;
import cn.gson.springboot.model.vo.DrugfyVo;
import cn.gson.springboot.model.vo.DrugqlVo;
import cn.gson.springboot.model.vo.DrugscheckVo;
import cn.gson.springboot.model.vo.InHospitalStorageVo;
import cn.gson.springboot.model.vo.OutpatientStorageVo;
import cn.gson.springboot.model.vo.ReturndrugVo;

@Controller
public class DrugRoomController {
	@Autowired
	private DrugRoomService drugservices;

	// 药房请领
	@RequestMapping("z-pharmacyinventory")
	public void z_pharmacyinventory() {

	}

	// 药房入库
	@RequestMapping("z-Instorage")
	public void z_Instorage() {

	}

	// 药房库存管理界面
	@RequestMapping("z-pharmacyingunli")
	public void kucunguanli() {

	}

	// 盘点界面
	@RequestMapping("z-check")
	public void pandian() {

	}

	// 药房退药界面
	@RequestMapping("z-drugroomReturngoods")
	public void tuiyao() {

	}

	@RequestMapping("z-dispensingshelves")
	public String fayao(Model model) {
		// 发药表随机数
		int hospid = (int) ((Math.random() * 4 + 1) * 100000);
		model.addAttribute("hospid", hospid);
		return "z-dispensingshelves";
	}

	// 药房请领查询药品基本信息
	@PostMapping(value = "selectAll-drugs")
	@ResponseBody
	public Map<String, Object> selectDrugInStorage(@RequestParam("page") int page, @RequestParam("rows") int rows,
			String drugname) {
		// 进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		// 进行数据格式转换和封装
		Map<String, Object> map = new HashMap<>();

		JSONArray selectDrugRoomInStorage;
		selectDrugRoomInStorage = drugservices.selectDrugRoomInStorage(null, null, null);
		if (drugname != null) {
			if (!drugname.equals("")) {
				JSONArray selectDrugRoomInStorage2 = drugservices.selectDrugRoomInStorage(drugname, null, null);
				if (selectDrugRoomInStorage2.size() > 0) {
					selectDrugRoomInStorage = drugservices.selectDrugRoomInStorage(drugname, null, null);
				}

				JSONArray selectDrugRoomInStorage3 = drugservices.selectDrugRoomInStorage(null, null, drugname);
				if (selectDrugRoomInStorage3.size() > 0) {
					selectDrugRoomInStorage = drugservices.selectDrugRoomInStorage(null, null, drugname);
				}
			}
		}

		map.put("total", pg.getTotal());// 封装总记录数
		map.put("rows", selectDrugRoomInStorage);// 封装当前页的内容

		return map;
	}

	// 请领新增入库
	@PostMapping(value = "save-drugInStorage")
	@ResponseBody
	public String addApply(DrugqlVo drugqlVo) {
		// 新增看诊----医嘱表--医嘱详情表
		try {
			drugservices.addApply(drugqlVo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}

	// 药房入库查询药品基本信息
	@PostMapping(value = "selectDrugs-InStorages")
	@ResponseBody
	public Map<String, Object> selectDrugsInStorage(@RequestParam("page") int page, @RequestParam("rows") int rows,
			String applytime) {
		// 进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		if (applytime != null) {
			if (applytime.equals("")) {
				// 时间查询
				applytime = null;

			}
		}
		JSONArray selectDrugRoomInStorage = drugservices.selectDrugsInStorage(applytime);
		// 进行数据格式转换和封装
		Map<String, Object> map = new HashMap<>();
		map.put("total", pg.getTotal());// 封装总记录数
		// System.out.println("1:"+pg.getTotal());
		map.put("rows", selectDrugRoomInStorage);// 封装当前页的内容
		// System.out.println("2:"+ list.size());
		return map;
	}

	// 药房入库查看详情--确认收货信息
	@PostMapping(value = "selectDrugs-InStoragesDetails")
	@ResponseBody
	public Map<String, Object> selectDrugsInStorageDetails(@RequestParam("page") int page,
			@RequestParam("rows") int rows, Integer drugapply_id) {

		// 进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray selectDrugsInStorageDetails = drugservices.selectDrugsInStorageDetails(drugapply_id, 0);

		// 进行数据格式转换和封装
		Map<String, Object> map = new HashMap<>();
		map.put("total", pg.getTotal());// 封装总记录数
		// System.out.println("1:"+pg.getTotal());
		map.put("rows", selectDrugsInStorageDetails);// 封装当前页的内容
		// System.out.println("2:"+ list.size());
		return map;
	}

	// 新增门诊药品入库
	@PostMapping(value = "Add-OutPatientStorage")
	@ResponseBody
	public String AddDrugRoomStorage(OutpatientStorageVo outvo, Integer drugApplyId, Integer outbound_number) {
		try {
			drugservices.saveDrugRoomStorage(outvo, drugApplyId, outbound_number);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return "2";
		}
	}

	// 新增住院药品入库
	@PostMapping(value = "Add-InHospitalStorage")
	@ResponseBody
	public String AddInHospitalStorage(InHospitalStorageVo inhosvo, Integer drugApplyId, Integer outbound_number) {
		try {
			drugservices.saveInHospitalStorage(inhosvo, drugApplyId, outbound_number);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
			return "2";
		}
	}

	// 已收货查看记录
	@PostMapping(value = "select-InstoragesRecordDetails")
	@ResponseBody
	public Map<String, Object> selectInstoragesRecordDetails(@RequestParam("page") int page,
			@RequestParam("rows") int rows, Integer drugapply_id) {

		// 进行分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray selectDrugsInStorageDetails = drugservices.selectDrugsInStorageDetails(drugapply_id, 1);

		// 进行数据格式转换和封装
		Map<String, Object> map = new HashMap<>();
		map.put("total", pg.getTotal());// 封装总记录数
		// System.out.println("1:"+pg.getTotal());
		map.put("rows", selectDrugsInStorageDetails);// 封装当前页的内容
		// System.out.println("2:"+ list.size());
		return map;
	}

	// 库存管理---门诊库存
	@PostMapping(value = "select_OutPatientStorage_record")
	@ResponseBody
	public Map<String, Object> select_OutPatientStorage_record(@RequestParam("page") int page,
			@RequestParam("rows") int rows, String drugname, Integer days) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_OutPatientStorage_record = drugservices.select_OutPatientStorage_record(drugname, days,
					null, null);
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			// System.out.println("1:"+pg.getTotal());
			map.put("rows", select_OutPatientStorage_record);// 封装当前页的内容
			// System.out.println("2:"+ list.size());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 库存管理---住院库存
	@PostMapping(value = "select_InhospitalStorage_record")
	@ResponseBody
	public Map<String, Object> select_InhospitalStorage_record(@RequestParam("page") int page,
			@RequestParam("rows") int rows, String drugname, Integer days) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_InhospitalStorage_record = drugservices.select_InhospitalStorage_record(drugname, days,
					null, null);
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			// System.out.println("1:"+pg.getTotal());
			map.put("rows", select_InhospitalStorage_record);// 封装当前页的内容
			// System.out.println("2:"+ list.size());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 门诊发药查询人员
	@PostMapping(value = "select_OutPatientStorage_people")
	@ResponseBody
	public Map<String, Object> select_OutPatientStorage_people(@RequestParam("page") int page,
			@RequestParam("rows") int rows, String clinic_name) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_OutPatientStorage_people = drugservices.select_OutPatientStorage_people(clinic_name);

			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_OutPatientStorage_people);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 门诊发药根据人员查询详情
	@PostMapping(value = "select_OutPatientStorage_peopledetails")
	@ResponseBody
	public Map<String, Object> select_OutPatientStorage_peopledetails(@RequestParam("page") int page,
			@RequestParam("rows") int rows, String cliniccard_id) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_OutPatientStorage_peopledetails = drugservices
					.select_OutPatientStorage_peopledetails(cliniccard_id);

			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_OutPatientStorage_peopledetails);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 门诊发药
	@PostMapping(value = "AddOutPatient_Publicdrug")
	@ResponseBody
	public String AddOutPatient_Publicdrug(DrugfyVo drugfyVo) {
		try {
			drugservices.AddOutPatient_Publicdrug(drugfyVo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}

	// 门诊发药记录人
	@PostMapping(value = "select_publicDrugPeople")
	@ResponseBody
	public Map<String, Object> select_publicDrugPeople(@RequestParam("page") int page, @RequestParam("rows") int rows,
			String clinic_name) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_publicDrugPeople = drugservices.select_publicDrugPeople(clinic_name);

			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_publicDrugPeople);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 门诊已经发药记录详情
	@PostMapping(value = "select_publicDrug")
	@ResponseBody
	public Map<String, Object> select_publicDrug(@RequestParam("page") int page, @RequestParam("rows") int rows,
			Integer publicdurg_id) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_publicDrug = drugservices.select_publicDrug(publicdurg_id);

			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_publicDrug);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	// 住院发药记录
	@PostMapping(value = "select_publicDrug_Inhospital")
	@ResponseBody
	public Map<String, Object> select_publicDrug_Inhospital(@RequestParam("page") int page,
			@RequestParam("rows") int rows, Integer publicdurg_type, Integer docdetails_type, Integer publicdurg_id) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_publicDrug_Inhospital = drugservices.select_publicDrug_Inhospital(publicdurg_type,
					docdetails_type, publicdurg_id);

			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_publicDrug_Inhospital);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 住院发药记录 查看详情
	@PostMapping(value = "select_publicDrug_InhospitalDetails")
	@ResponseBody
	public Map<String, Object> select_publicDrug_InhospitalDetails(@RequestParam("page") int page,
			@RequestParam("rows") int rows, Integer publicdurg_type, Integer publicdurg_id, Integer docdetails_type) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray select_publicDrug_InhospitalDetails = drugservices
					.select_publicDrug_InhospitalDetails(publicdurg_type, publicdurg_id, docdetails_type);
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_publicDrug_InhospitalDetails);// 封装当前页的内容
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 住院发药
	@PostMapping(value = "addoutDrug-Inhospital")
	@ResponseBody
	public String outDrugInhospital(DrugfyVo dvo) {
		try {
			drugservices.outDrugInhospital(dvo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}

	// 门诊与住院库存不足申请领药
	@PostMapping(value = "apply_drugMessage")
	@ResponseBody
	public JSONArray apply_drugMessage(String drug_number_arr) {
		try {
			// System.out.println("drug_number_arr----------:"+drug_number_arr);
			// JSONArray select_OutPatientStorage_record = null;
			String drug_number[] = drug_number_arr.split(",");
			JSONArray json = new JSONArray();
			for (int i = 0; i < drug_number.length; i++) {
				JSONArray select_OutPatientStorage_record = drugservices.selectDrugRoomInStorage(null,
						Integer.valueOf(drug_number[i]), null);
				json.add(select_OutPatientStorage_record.get(0));
				// System.out.println("select_OutPatientStorage_record-----------------P:"+select_OutPatientStorage_record);
			}
			// System.out.println("json-----------------P:"+json);

			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 门诊退药
	@PostMapping(value = "return_goods_goodsMessage")
	@ResponseBody
	public JSONArray return_goods_goodsMessage(String drug_number_arr, String drug_pici_arr) {
		try {
			// System.out.println("drug_number_arr:"+drug_number_arr+"--------drug_pici_arr:"+drug_pici_arr);
			String drug_number[] = drug_number_arr.split(",");
			String drug_pici[] = drug_pici_arr.split(",");
			JSONArray json = new JSONArray();
			for (int i = 0; i < drug_number.length; i++) {
				// JSONArray select_OutPatientStorage_record =
				// drugservices.selectDrugRoomInStorage(null,Integer.valueOf( drug_number[i]));
				// 首先根据传过来的药品id与批次查询出门诊库存----得到这个药品库存数量
				JSONArray select_OutPatientStorage_record = drugservices.select_OutPatientStorage_record(null, null,
						Integer.valueOf(drug_number[i]), Integer.valueOf(drug_pici[i]));
				// System.out.println("select_OutPatientStorage_record----门诊:"+select_OutPatientStorage_record);
				// 根据药品编号与批次查询药库库存----查到inventory_number
				for (int j = 0; j < select_OutPatientStorage_record.size(); j++) {
					Integer drugnumber = select_OutPatientStorage_record.getJSONObject(j).getInteger("drug_number");
					Integer outpatient_pici = select_OutPatientStorage_record.getJSONObject(j)
							.getInteger("outpatient_pici");
					// System.out.println("drugnumber:"+drugnumber+"-----------outpatient_pici:"+outpatient_pici);
					// 查询的库存id
					JSONArray select_inventoryDrug_number = drugservices.select_inventoryDrug_number(drugnumber,
							outpatient_pici);
					Integer inventory_number = select_inventoryDrug_number.getJSONObject(j)
							.getInteger("inventory_number");
					// System.out.println("inventory_number:"+inventory_number);
					// 将库存id加入到查出的药品信息里面
					JSONObject object = (JSONObject) select_OutPatientStorage_record.get(j);
					object.put("inventory_number", inventory_number);
					// System.out.println("select_OutPatientStorage_record----门诊加入库存id之后:"+select_OutPatientStorage_record);
					json.add(object);
				}
			}
			// System.out.println("end--json:"+json);

			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 药房门诊退药新增---退给药库
	@PostMapping(value = "save-Returngoods")
	@ResponseBody
	public String save_Returngoods(ReturndrugVo ReturndrugVo) {
		try {
			drugservices.saveReturn(ReturndrugVo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}

	// 住院退药
	@PostMapping(value = "return_goods_goods_inhospital")
	@ResponseBody
	public JSONArray return_goods_goods_inhospital(String drug_number_arr, String drug_pici_arr) {
		try {
			// System.out.println("住院drug_number_arr:"+drug_number_arr+"住院--------drug_pici_arr:"+drug_pici_arr);
			String drug_number[] = drug_number_arr.split(",");
			String drug_pici[] = drug_pici_arr.split(",");
			JSONArray json = new JSONArray();
			for (int i = 0; i < drug_number.length; i++) {
				// JSONArray select_OutPatientStorage_record =
				// drugservices.selectDrugRoomInStorage(null,Integer.valueOf( drug_number[i]));
				// 首先根据传过来的药品id与批次查询出门诊库存----得到这个药品库存数量
				JSONArray select_InhospitalStorage_record = drugservices.select_InhospitalStorage_record(null, null,
						Integer.valueOf(drug_number[i]), Integer.valueOf(drug_pici[i]));
				// System.out.println("select_OutPatientStorage_record----住院:"+select_InhospitalStorage_record);
				// 根据药品编号与批次查询药库库存----查到inventory_number
				for (int j = 0; j < select_InhospitalStorage_record.size(); j++) {
					Integer drugnumber = select_InhospitalStorage_record.getJSONObject(j).getInteger("drug_number");
					Integer outpatient_pici = select_InhospitalStorage_record.getJSONObject(j)
							.getInteger("inhospital__pici");
					// System.out.println("drugnumber:"+drugnumber+"住院-----------outpatient_pici:"+outpatient_pici);
					// 查询的库存id
					JSONArray select_inventoryDrug_number = drugservices.select_inventoryDrug_number(drugnumber,
							outpatient_pici);
					Integer inventory_number = select_inventoryDrug_number.getJSONObject(j)
							.getInteger("inventory_number");
					// System.out.println("inventory_number:"+inventory_number);
					// 将库存id加入到查出的药品信息里面
					JSONObject object = (JSONObject) select_InhospitalStorage_record.get(j);
					object.put("inventory_number", inventory_number);
					// System.out.println("select_OutPatientStorage_record----住院加入库存id之后:"+select_InhospitalStorage_record);
					json.add(object);
				}
			}
			// System.out.println("住院end--json:"+json);

			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 门诊药房退药新增---退给药库
	@PostMapping(value = "save-Returngoods-inhospital")
	@ResponseBody
	public String save_Returngoods_inhospital(ReturndrugVo ReturndrugVo) {
		try {
			drugservices.saveInhosptialReturn(ReturndrugVo);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}

	// 退药记录
	@PostMapping(value = "select-returnGoodsRecord")
	@ResponseBody
	public JSONArray select_returnGoodsRecord(Integer return_goods_id) {
		try {
			return drugservices.select_returnDrug_record(return_goods_id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@PostMapping(value = "select_returnDrug_recordDetails")
	@ResponseBody
	public JSONArray select_returnDrug_recordDetails(Integer return_goods_id) {
		try {
			JSONArray select_returnDrug_recordDetails = drugservices.select_returnDrug_recordDetails(return_goods_id);
			// System.out.println("select_returnDrug_recordDetails---------------:"+select_returnDrug_recordDetails);
			return select_returnDrug_recordDetails;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 门诊盘点
	@PostMapping(value = "Check_OutPatientStorage_record")
	@ResponseBody
	public Map<String, Object> Check_OutPatientStorage_record(@RequestParam("page") int page,
			@RequestParam("rows") int rows,Integer drug_number,Integer drug_pici) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			// 门诊发药
			Integer outpatient_publicdrugamount = null;
			//退药
			Integer return_goods_allquantity = null;
			//销毁
			Integer destroy_allquantity=null;
			//出库
			Integer outpatient_outamount = 0;
			// 门诊入库
			Integer outpatient_inamount = null;
			// 药品信息
			JSONArray select_OutPatientStorage_record = drugservices.select_CheckOutPatientStorage_record(drug_number,drug_pici);
			
			// 药品id查出出库与入库数量
			for (int i = 0; i < select_OutPatientStorage_record.size(); i++) {
				// 药品id
				Integer drug_id = select_OutPatientStorage_record.getJSONObject(i).getInteger("drug_number");
				// 药品批次
				Integer drug_picis = select_OutPatientStorage_record.getJSONObject(i).getInteger("outpatient_pici");
				System.out.println("药品id:"+drug_id);

				//发药
				JSONArray select_outpatient_outAmount = drugservices.select_outpatient_outAmount(drug_id,0,drug_picis);
				if (select_outpatient_outAmount.size() > 0) {
					outpatient_publicdrugamount = select_outpatient_outAmount.getJSONObject(0).getInteger("out_doorshuliang");
				} else {
					outpatient_publicdrugamount = 0;
				} 
				
				//退药
				JSONArray return_good_amountBydrug_number_pici = drugservices.return_good_amountBydrug_number_pici("门诊药房退药", drug_id,drug_picis);
				if (return_good_amountBydrug_number_pici.size() > 0) {
					return_goods_allquantity = return_good_amountBydrug_number_pici.getJSONObject(0).getInteger("return_goods_allquantity");
				} else {
					return_goods_allquantity = 0;
				}
				System.out.println("退药:"+return_goods_allquantity);
				
				//销毁
				JSONArray destroy_good_amountBydrug_number_pici = drugservices.destroy_good_amountBydrug_number_pici(4,drug_id,drug_picis);
				if (destroy_good_amountBydrug_number_pici.size() > 0) {
					destroy_allquantity = destroy_good_amountBydrug_number_pici.getJSONObject(0).getInteger("destroy_allquantity");
				} else {
					destroy_allquantity = 0;
				}
				//System.out.println("销毁:"+destroy_good_amountBydrug_number_pici.getJSONObject(0).getInteger("drug_number")+":=---:"+destroy_allquantity);
				/*System.out.println("drug_id---:"+drug_id
			    +"---------名字"+select_OutPatientStorage_record.getJSONObject(i).getString("drug_name")
			    +"---------批次"+select_OutPatientStorage_record.getJSONObject(i).getInteger("outpatient_pici"));*/
				outpatient_outamount=return_goods_allquantity+outpatient_publicdrugamount+destroy_allquantity; 

				// 入库
				JSONArray select_outpatient_InAmount = drugservices.select_outpatient_InAmount(drug_id,1,drug_picis);
				// System.out.println("select_outpatient_InAmount数组:"+select_outpatient_InAmount);
				// System.out.println("select_outpatient_InAmount入库查出大小:"+select_outpatient_InAmount.size());

				if (select_outpatient_InAmount.size() > 0) {
					outpatient_inamount = select_outpatient_InAmount.getJSONObject(0).getInteger("in_doorshuliang");
				} else {
					outpatient_inamount = 0;
				}
				// System.out.println("门诊药品id："+drug_id+"-------------出库数量:"+outpatient_outamount+"-------------入库数量:"+outpatient_inamount);

				JSONObject obj = (JSONObject) select_OutPatientStorage_record.get(i);
				obj.put("outpatient_outamount", outpatient_outamount);
				obj.put("outpatient_inamount", outpatient_inamount);
				//System.out.println("门诊加入-------------出入库数量:"+obj);
			}
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_OutPatientStorage_record);// 封装当前页的内容
			//System.out.println("map:"+map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 住院盘点
	@PostMapping(value = "check_InhospitalStorage_record")
	@ResponseBody
	public Map<String, Object> check_InhospitalStorage_record(@RequestParam("page") int page,
						@RequestParam("rows") int rows,Integer drug_number,Integer drug_pici) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			// 出库
			Integer Inhospital_outAmount = null;
			// 入库
			Integer Inhospital_inAmount = null;
			// 查询药品信息
			JSONArray select_InhospitalStorage_record = drugservices.select_CheckInhospitalStorage_record(drug_number,drug_pici);
			// 药品id查出出库与入库数量
			for (int i = 0; i < select_InhospitalStorage_record.size(); i++) {
				// 药品id
				Integer drug_id = select_InhospitalStorage_record.getJSONObject(i).getInteger("drug_number");
				// 药品批次
				Integer drug_picis = select_InhospitalStorage_record.getJSONObject(i).getInteger("inhospital__pici");
				
				// 出库
				JSONArray select_Inhospital_outAmount = drugservices.select_outpatient_outAmount(drug_id, 2,drug_picis);
				if (select_Inhospital_outAmount.size() > 0) {
					Inhospital_outAmount = select_Inhospital_outAmount.getJSONObject(0).getInteger("out_doorshuliang");
				}else {
					Inhospital_outAmount = 0;
				}
				// 入库
				JSONArray select_Inhospital_InAmount = drugservices.select_outpatient_InAmount(drug_id, 2,drug_picis);
				if (select_Inhospital_InAmount.size() > 0) {
					Inhospital_inAmount = select_Inhospital_InAmount.getJSONObject(0).getInteger("in_doorshuliang");
				} else {
					Inhospital_inAmount = 0;
				}

				JSONObject obj = (JSONObject) select_InhospitalStorage_record.get(i);
				obj.put("Inhospital_outAmount", Inhospital_outAmount);
				obj.put("Inhospital_inAmount", Inhospital_inAmount);
				// System.out.println("住院加入-------------出入库数量:"+obj);

			}
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			map.put("total", pg.getTotal());// 封装总记录数
			map.put("rows", select_InhospitalStorage_record);// 封装当前页的内容

			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 门诊查询亏损出入库记录
	@PostMapping(value = "select_oupatient_badamount")
	@ResponseBody
	public Map<String, Object> select_oupatient_badamount(@RequestParam("page") int page,
																						@RequestParam("rows") int rows,
																						String drug_number_arr) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			// 进行数据格式转换和封装
			Map<String, Object> map = new HashMap<>();
			// 门诊出库
			Integer outpatient_outamount = null;
			// 门诊入库
			Integer outpatient_inamount = null;
			String drugnumber_arr[]=drug_number_arr.split(",");
           
			JSONArray json=new JSONArray();
			JSONArray select_OutPatientStorage_record=null;
			// 药品信息
			if(!drugnumber_arr.equals("")) {	
				for (int t= 0; t < drugnumber_arr.length; t++) {
				select_OutPatientStorage_record=drugservices.select_OutPatientStorage_record(null, null,Integer.valueOf(drugnumber_arr[t]), null);
				
				// 药品id查出出库与入库数量
				for (int i = 0; i < select_OutPatientStorage_record.size(); i++) {
					// 药品id
					Integer drug_id = select_OutPatientStorage_record.getJSONObject(i).getInteger("drug_number");
					
					Integer outpatient_pici = select_OutPatientStorage_record.getJSONObject(i).getInteger("outpatient_pici");


					// 入库
					JSONArray select_oupatient_bad_in_amount = drugservices.select_oupatient_bad_in_amount(1, drug_id,outpatient_pici);
					// System.out.println("select_outpatient_InAmount数组:"+select_outpatient_InAmount);
					// System.out.println("select_outpatient_InAmount入库查出大小:"+select_outpatient_InAmount.size());

					if (select_oupatient_bad_in_amount.size() > 0) {
						outpatient_inamount = select_oupatient_bad_in_amount.getJSONObject(0).getInteger("dp_details_shijishuliang");
					} else {
						outpatient_inamount = 0;
					}
					// System.out.println("门诊药品id："+drug_id+"-------------出库数量:"+outpatient_outamount+"-------------入库数量:"+outpatient_inamount);
                     
					// 出库
					JSONArray select_outpatient_outAmount = drugservices.select_outpatient_outAmount(drug_id,0,outpatient_pici);
					if (select_outpatient_outAmount.size() > 0) {
						outpatient_outamount = select_outpatient_outAmount.getJSONObject(0).getInteger("out_doorshuliang");
					} else {
						outpatient_outamount = 0;
					}  
					
					JSONObject obj = (JSONObject) select_OutPatientStorage_record.get(i);
					obj.put("outpatient_outamount", outpatient_outamount);
					obj.put("outpatient_inamount", outpatient_inamount);
					json.add(obj);
				  }     
				}
				map.put("total", pg.getTotal());// 封装总记录数
				map.put("rows",json);// 封装当前页的内容
				System.out.println("map出入库数量:"+map);
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

	// 住院查询亏损出入库记录
	@PostMapping(value = "select_Inhospital_badamount")
	@ResponseBody
	public Map<String, Object> select_Inhospital_badamount(@RequestParam("page") int page,
																						 @RequestParam("rows") int rows,
																						 String drug_number_arrs) {
		try {
			// 进行分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			Map<String, Object> map = new HashMap<>();
			
			// 住院出库
			Integer outpatient_outamount = null;
			// 住院入库
			Integer outpatient_inamount = null;
			
			String drugnumber_arr[]=drug_number_arrs.split(",");
	        //System.out.println("drug_number_arrs;"+drug_number_arrs);
			JSONArray json=new JSONArray();
			JSONArray select_InhospitalStorage_record=null;
			if(!drugnumber_arr.equals("")) {	
				for (int t= 0; t < drugnumber_arr.length; t++) {
					// 药品信息
				    select_InhospitalStorage_record = drugservices.select_InhospitalStorage_record(null, null,Integer.valueOf(drugnumber_arr[t]),null);
				}
				
				// 药品id查出出库与入库数量
				for (int i = 0; i < select_InhospitalStorage_record.size(); i++) {
					// 药品id
					Integer drug_id = select_InhospitalStorage_record.getJSONObject(i).getInteger("drug_number");
					Integer inhospital__pici= select_InhospitalStorage_record.getJSONObject(i).getInteger("inhospital__pici");
					// 出库
					JSONArray select_oupatient_badamount = drugservices.select_oupatient_badamount(2, drug_id,inhospital__pici);
					// System.out.println("门诊select_oupatient_badamount:"+select_oupatient_badamount);
					if (select_oupatient_badamount.size() > 0) {
						outpatient_outamount = select_oupatient_badamount.getJSONObject(0).getInteger("pd_details_sjcount");
					} else {
						outpatient_outamount = 0;
					}

					// 入库
					JSONArray select_oupatient_bad_in_amount = drugservices.select_oupatient_bad_in_amount(2, drug_id,inhospital__pici);
					// System.out.println("select_outpatient_InAmount数组:"+select_outpatient_InAmount);
					// System.out.println("select_outpatient_InAmount入库查出大小:"+select_outpatient_InAmount.size());

					if (select_oupatient_bad_in_amount.size() > 0) {
						outpatient_inamount = select_oupatient_bad_in_amount.getJSONObject(0).getInteger("dp_details_shijishuliang");
					} else {
						outpatient_inamount = 0;
					}
					// System.out.println("门诊药品id："+drug_id+"-------------出库数量:"+outpatient_outamount+"-------------入库数量:"+outpatient_inamount);

					JSONObject obj = (JSONObject) select_InhospitalStorage_record.get(i);
					obj.put("hospital_outamount", outpatient_outamount);
					obj.put("hospital_inamount", outpatient_inamount);
					json.add(obj);
				}
				
				map.put("total", pg.getTotal());// 封装总记录数
				map.put("rows", json);// 封装当前页的内容
			}
			return map;
		} catch (Exception e) { 
			e.printStackTrace();
			return null;
		}
	}
	
	//药品销毁
	@PostMapping(value = "save-Destructions")
	@ResponseBody
	public String saveDestruction(DestructionVo DestructionVo,String return_id) {
		try {
			drugservices.saveDestructions(DestructionVo,return_id);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}
	
	//统计退药数量 
	@PostMapping(value="return_good_amount")
	@ResponseBody
	public String return_good_amount(String return_goods_type,Integer drug_number,Integer return_details_pici) {
		try {
			JSONArray return_good_amountBydrug_number_pici = drugservices.return_good_amountBydrug_number_pici(return_goods_type, drug_number, return_details_pici);
			return return_good_amountBydrug_number_pici.toJSONString();	
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//统计销毁数量 
	@PostMapping(value="destroy_good_amount")
	@ResponseBody
	public String destroy_good_amount(Integer destroy_state,Integer drug_number,Integer destroy_pici){
		try {
			JSONArray destroy_good_amountBydrug_number_pici = drugservices.destroy_good_amountBydrug_number_pici(destroy_state, drug_number, destroy_pici);
					
			return destroy_good_amountBydrug_number_pici.toJSONString();	
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@PostMapping(value="insert_checks")
	@ResponseBody
	public String add_checks(DrugscheckVo chekvo) {
		try {
			drugservices.add_checks(chekvo);
			return "1";	
			
		} catch (Exception e) {
			e.printStackTrace();
			return "2";
		}
	}
	
	//查看盘点
	@PostMapping(value="select_check")
	@ResponseBody
	public JSONArray select_check() {
		try {
			
		     return drugservices.select_check();
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
			   
	//盘点查看详情
	@PostMapping(value="select_checkDetails")
	@ResponseBody
	public JSONArray select_checkDetails(Integer drugchek_id) {
		try {
		  return drugservices.select_checkDetails(drugchek_id);
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
